//文件上传
//导入express
const express = require("express")
//创建Router实例
const router = express.Router()

const path = require("path")

const {upload} = require("../utils/fileUpload");
const {filePath} = require("../config");

//单文件上传
router.post("/upload", upload.single("file"), (req, res) => {
    res.send({
        status: 0,
        message: "文件上传成功",
        data: req.file.filename,
    })
})

//多文件上传

router.post("/multipleUpload", upload.array(['files']), (req, res) => {
    let files = req.files.map(item => item.filename)
    res.send({
        status: 1,
        message: "文件上传成功",
        data: files
    })
})

// 预览上传的图片
router.get(`/${filePath}:filename`, (req, res) => {
    let filename = path.join(path.resolve(__dirname), '../', filePath, req.params.filename);
    res.sendFile(filename);
});

//导出router模块
module.exports = router;

